<?php
/**
 *
 * User: zhan
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\controller;


use think\Db;

class Set extends Common{

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}
	/**
	 * 设置
	 */
	public function index(){
		$list=db("config")->select();
		$this->assign('list',$list);
		return $this->fetch();
	}
	/**
	 * 设置
	 */
	public function deal(){
		$name=input('name');
		$value=input('value');
		if(!$name){echo -1;exit;};
		$where['id']=$name;
		$list=db("config")->where($where)->find();
		if(!$list) {echo -2;exit;};
		$res=db("config")->where($where)->update(array('value'=>$value));
		if($res===false) {echo -3;exit;};
		echo 1;exit;
	}
	/**
	* 店铺设置
	*/
	public function shop_list(){
		$status=input("status");
		if($status!=''){
			$where['status']=$status;
		}else{
			$where['status']=array('gt',-2);
		}
		$type=input("type");
		if($type!=''){
			$where['type']=$type;
		}
		if(input("keyword")){
			$where['nickname|mobile']=array("like",'%'.input('keyword').'%');
		}
		if(input("export")){
			$this->export($where);exit;
		}
		$db_shop=db("shop");
		$list = $db_shop->where($where)->order("sort desc,id desc")->paginate(10);
		$count =$db_shop->where($where)->order("id desc")->count();

		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('status',input("status"));
		$this->assign('type',input("type"));
		$this->assign('keyword',input("keyword"));
		return $this->fetch();
	}

	/**
	 * 轮播图
	 */
	public function banner(){
		$id = input('shop_id');
		$banner = db('banner_index');
		$ban = $banner->where(['shop_id'=>$id])->order('sort desc')->select();
		$shop = db('shop')->where(['id'=>$id])->find();

		foreach ($ban as $key => &$value) {
			$category_name = db('category_goods')->where(['shop_id'=>$value['shop_id'],'id'=>$value['cid']])->value('name');
			$value['cateogry_name'] = $category_name;
		}

		$this->assign("list",$ban);
		$this->assign("id",$id);
		$this->assign("shop",$shop);
		return $this->fetch();
	}
	/**
	 * 意见反馈
	 */
	public function feedback(){
		$feedback = db('feedback');
		$list = $feedback->order("id desc")->paginate(10);
		$list->each(function($i){
			$ims = unserialize($i['images']);
			//var_dump($ims);
			if($ims[0]) {
				//$ims = unserialize($i['images']);
				//var_dump($ims);
				$images = array();
				foreach ($ims as $v) {
					$v = json_decode($v,true);
					//var_dump($v);
					if(count($v)>1){
						foreach($v as $k){
							//var_dump($k);
							$images[] = IMG_PATH . $k;
						}
					}	
				}
				$i['images'] = $images;
			}
			return $i;
		});
		$count = $feedback->order("id desc")->count();
		$this -> assign("list",$list);
		$this -> assign("count",$count);
		return $this->fetch();
	}
	/**
	 * 意见反馈
	 */
	public function feedback_detail(){
		$id = input('id');
		$feedback = db('feedback');
		$list = $feedback->where("id=".$id)->find();
		$ims = unserialize($list['images']);
		if($ims[0]) {
			//$ims = unserialize($i['images']);
			//var_dump($ims);
			$images = array();
			foreach ($ims as $v) {
				$v = json_decode($v,true);
				//var_dump($v);
				if(count($v)>1){
					foreach($v as $k){
						//var_dump($k);
						$images[] = IMG_PATH . $k;
					}
				}	
			}
		}
		$list['images'] = $images;
		$this -> assign("list",$list);
		return $this->fetch();
	}
	/**
	 * 轮播添加或修改
	 */
	public function banner_edit(){
		$id = input('id');
		$shop_id = input('shop_id');
		if(request()->isPost()){
			$data['status'] = input('post.status');
			$data['cid'] = input('post.cid');
			$data['position'] = input('post.position');
			$data['position'] = input('post.position');
			$data['title'] = input('post.title');
			$data['sort'] = input('post.displayorder');
			$data['url'] = input('post.url');
			$data['shop_id'] = input('post.shop_id');
			$file = request()->file('bnimage');
			if (!empty($file)) {
				$info = $file->move('./static/uploads');
				if ($info) {
					$data['pic'] = $info->getSaveName();
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['pic'] = input('bne');
			}
			if($id){
				$bnr = db('banner_index')->where("id='$id'")->update($data);
			}else{
				$bnr = db('banner_index')->insert($data);
			}//redirect
			$this->redirect(url('Set/banner',['shop_id'=>$shop_id]));  //url('Set/banner'));
		}
		$banner=db('banner_index')->where("id='$id'")->find();
		$category_goods = db('category_goods')->where(['shop_id'=>$shop_id])->select();
		//$shop_list = db('shop')->field('shopname,id')->select();

		//$this->assign('shop_list',$shop_list);
		$this->assign('category_goods',$category_goods);
		$this->assign("banner",$banner);
		$this->assign("shop_id",$shop_id);
		return $this->fetch();

	}
	/**
	 * 轮播删除
	 */
	public function banner_del(){
		$bnid = input('id');
		$banner = db('banner_index');
		$del = $banner -> where("id='$bnid'") -> delete();
		if($del){
			exit(json_encode(1));
		}else{
			exit(json_encode("删除失败"));
		}
	}
	/**
	 * 意见反馈
	 */
	public function feedback_del(){
		$bnid = input('id');
		$feedback = db('feedback');
		$del = $feedback -> where("id='$bnid'") -> delete();
		if($del){
			exit(json_encode(1));
		}else{
			exit(json_encode("删除失败"));
		}
	}
	/*
		店铺分类列表
	 */
	public function category()
	{
		$where=array();
		$keyword = input('keyword');
		if($keyword){
			$where['name']=array("like",'%'.$keyword.'%');
		}
		$category=db("category_shop");
		$list = $category->where($where)->order("sort desc")->select();
		$this->assign('list',$list);
		$count =$category->where($where)->count();
		$this->assign('keyword',$keyword);
		$this->assign('count',$count);
		return $this->fetch();
	}
	/*
		添加商品分类
	 */
	public function add_category()
	{

		$id = input('id/d');
		$data = array();
		$category=db("category_shop");
		if(request()->isPost()){
			$data['name']=input("name");
			$data['sort']=input("sort");
			$file = request()->file('image');
			if($file){
				$info = $file->move('./static/uploads');
				if($info){
					$data['image']= $info->getSaveName();
				}
			}
			if(!empty($id)){
				$data['status'] = input("status");
				$res=$category->where("id='$id'")->update($data);
			}else{
				$res=$category->insert($data);
			}
			if(!$res) $this->error("操作失败！");
			$this->redirect(url('Set/category'));
		}

		$goods_result = $category->where("id='$id'")->find();
		$this->assign('goods_result',$goods_result);
		return $this->fetch();
	}
	/*
		删除店铺分类
	 */
	public function del_category()
	{
		$id = input('id/d');
		$category=db("category_shop");
		$goods=db("shop");
		$result =$goods->where("cid='$id'") ->find();
		if($result) {
			exit(json_encode("该分类已存在商家，不能删除"));
		}
		$del =$category->where("id='$id'") ->delete();;
		if($del){
			exit(json_encode(1));
		}else{
			exit(json_encode("删除失败"));
		}
	}

	/*
	* 关于我们
	*/
	public function about(){
		$id = 2;
		if(request()->isPost()){
			$file = request()->file('image');
			if($file){
				$info = $file->move('./static/uploads');
				if($info){
					$data['image']= $info->getSaveName();
				}
			}
			$data['content']= htmlspecialchars(input('content'));
			$data['mobile']= input('mobile');
			if(db('contract')->where(['id' => $id])->update($data) === false) {
				$this->error('更新失败');
			}else{
				$this->redirect($_SERVER['HTTP_REFERER']);
			}
		}
		$detail=db("contract")->where("id='$id'")->find();
		if(!$detail) $this->error("记录不存在");
		$this->assign("data",$detail);
		return $this->fetch();
	}
	/*
	* 活动专区
	*/
	public function activity(){
		$shop_id = input('shop_id');
		$activity=db("activity_index");
		$list = $activity->where(['shop_id'=>$shop_id])->order('sort desc')->select();

		foreach ($list as $key => &$value) {
			$name = db('category_goods')->where(['id'=>$value['cid']])->value('name');
			$value['category_name'] = $name;
		}

		$shopname = db('shop')->where(['id'=>$shop_id])->value('shopname');

		$this->assign('shopname',$shopname);
		$this->assign('id',$shop_id);
		$this->assign('list',$list);
		return $this->fetch();
	}
	/*
	* 活动专区
	*/
	public function activity_edit(){
		$shop_id=input("shop_id");
		$id = input("id/d");
		$activity=db("activity_index");
		$detail=$activity->where("id='$id'")->find();
		if(request()->isPost()){
			$file = request()->file('image');
			if($file){
				$info = $file->move('./static/uploads');
				if($info){
					$data['image']= $info->getSaveName();
				}
			}
			$data['title']= input('title');
			$data['cid'] = input('cid');
			$data['sort'] = input('sort');
			$data['shop_id'] = $shop_id;
			
			if(!empty($id)){
				$activity->where(['id'=>$id])->update($data);
			}else{
				$activity->insert($data);
			}

			$this->redirect(url('Set/activity',['shop_id'=>$shop_id]));
		}
		
		$category_goods = db('category_goods')->where(['shop_id'=>$shop_id])->select();

		$this->assign("data",$detail);
		$this->assign("shop_id",$shop_id);
		$this->assign("category_goods",$category_goods);
		return $this->fetch();
	}

	/*
	* 设置活动
	*/
	public function activity_set(){
		if(request()->isPost()){
			$activity_list=db('activity_list');
			$data['aid']=input("aid/d");
			$data['province']=input('province');
			$data['city']=input('city');
			$data['county']=input('county');
			$shop_id=input('shop_id/a');
			if(!$shop_id){
				$this->error('请选择店铺');
			}
			$goods_id=input('goods_id/a');
			foreach($shop_id as $k=> $v){
				$data['shop_id']=$v;
				$data['goods_id']=$goods_id[$k];
				$activity_list->insert($data);
			}
			$this->redirect(url("set/activity_list",array('id'=>$data['aid'])));
		}
		$this->assign("aid",input("aid/d"));
		return $this->fetch();
	}

	/**
	 *   店铺
	 */
	public function tpl(){
		$where['county']=input("county");
		$where['status']=1;
		$shop= db("shop")->where($where)->order("id desc")->select();
		if(!$shop){exit(0);}
		$this->assign('shop',$shop);
		return $this->fetch();
	}
	/**
	 *   商品
	 */
	public function goodstpl(){
		$where['county']=input("county");
		$where['status']=1;
		$shop= db("good")->where($where)->order("id desc")->select();
		if(!$shop){exit(0);}
		$this->assign('shop',$shop);
		return $this->fetch();
	}
	public function searchgoods()
	{
     	$where['shop_id'] = input("shop_id");
		$where['status'] = 1;
		$goods = db("goods")->where($where)->order("id desc")->select();
		$str="";
		foreach($goods as $v){
			$str.= "<option value='".$v['id']."'>".$v['name']."</option>";
		}
		echo $str;
	}
	/**
	 *   搜索店铺
	 */
	public function searchshop(){
		$key=input("key");
		$where['shopname|mobile']=array("like",'%'.$key.'%');
		$where['status']=1;
		$data=db("shop")->where($where)->select();
		$str="";
		foreach($data as $v){
			$str.= "<option value='".$v['id']."'>".$v['shopname'].'-'.$v['mobile']."</option>";
		}
		echo $str;
	}
	/*
    删除店铺分类
 */
	public function activity_del()
	{
		$id = input('id/d');
		$category=db("activity_list");
		$goods=db("shop");
		$result =$goods->where("cid='$id'") ->find();
		if($result) {
			exit(json_encode("该分类已存在商家，不能删除"));
		}
		$del =$category->where("id='$id'") ->delete();;
		if($del){
			exit(json_encode(1));
		}else{
			exit(json_encode("删除失败"));
		}
	}
	/**
	 * 轮播图2
	 */
	public function banner2(){
		$banner = db('banner2');
		$ban = $banner->select();
		$this -> assign("list",$ban);
		return $this->fetch();
	}
	/**
	 * 轮播添加或修改
	 */
	public function banner_edit2(){
		$id = input('id');
		if(request()->isPost()){
			$data['url'] = input('post.url');
			$data['shop_id'] = input('post.shop_id');
			$file = request()->file('bnimage');
			if (!empty($file)) {
				$info = $file->move('./static/uploads');
				if ($info) {
					$data['img'] = $info->getSaveName();
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['img'] = input('bne');
			}
			if($id){
				$bnr = db('banner2')->where("id='$id'")->update($data);
			}else{
				$bnr = db('banner2')->insert($data);
			}//redirect
			$this->redirect(url('Set/banner2'));  //url('Set/banner'));
		}
		$banner=db('banner2')->where("id='$id'")->find();
		$shop_list = db('shop')->field('shopname,id')->select();

		$this->assign('shop_list',$shop_list);
		$this -> assign("banner",$banner);
		return $this->fetch();

	}
	/**
	 * 轮播删除
	 */
	public function banner_del2(){
		$bnid = input('id');
		$banner = db('banner2');
		$del = $banner -> where("id='$bnid'") -> delete();
		if($del){
			exit(json_encode(1));
		}else{
			exit(json_encode("删除失败"));
		}
	}
	/*
	* 活动专区2
	*/
	public function activity2(){
		$activity=db("activity2");
		$list = $activity->select();
		foreach ($list as $key => &$value) {
			$shop_name = db('shop')->where(['id'=>$value['shop_id']])->field('shopname')->find();
			$value['shop_id'] = $shop_name['shopname'];
		}
		$this->assign('list',$list);
		return $this->fetch();
	}
	/**
	 * 活动添加或修改
	 */
	public function activity_edit2(){
		$id = input('id');
		if(request()->isPost()){
			$data['shop_id'] = input('post.shop_id');
			$file = request()->file('bnimage');
			if (!empty($file)) {
				$info = $file->move('./static/uploads');
				if ($info) {
					$data['pic'] = $info->getSaveName();
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['pic'] = input('bne');
			}
			if($id){
				$bnr = db('activity2')->where("id='$id'")->update($data);
			}else{
				$bnr = db('activity2')->insert($data);
			}//redirect
			$this->redirect(url('Set/activity2'));  //url('Set/banner'));
		}
		$banner=db('activity2')->where("id='$id'")->find();
		$shop = db('shop')->where(['status'=>'1','ishome'=>'1'])->select();

		$this->assign("banner",$banner);
		$this->assign("shop",$shop);
		return $this->fetch();

	}
	/**
	 * 活动删除
	 */
	public function activity_del2(){
		$bnid = input('id');
		$banner = db('activity2');
		$del = $banner -> where("id='$bnid'") -> delete();
		if($del){
			exit(json_encode(1));
		}else{
			exit(json_encode("删除失败"));
		}
	}

}